/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package messageexample;

import java.util.logging.Level;
import java.util.logging.Logger;
import javax.annotation.Resource;
import javax.ejb.ActivationConfigProperty;
import javax.ejb.MessageDriven;
import javax.ejb.MessageDrivenContext;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.TextMessage;

/**
 *
 * @author v
 */
@MessageDriven(mappedName = "jms/messages")
public class NewMessageBean implements MessageListener {
    static final Logger logger = Logger.getLogger("NewMessageBean");
    
    @Resource
    private MessageDrivenContext mdc;
    
    public NewMessageBean() {
    }

    public void onMessage(Message message) {
        TextMessage msg = null;

        try {
            if (message instanceof TextMessage) {
                msg = (TextMessage) message;
                logger.log(Level.INFO, "NewMessageBean: Message recived: {0}", msg.getText());
            } else {
                logger.log(Level.WARNING, "Wrong message type: {0}", message.getClass().getName());
            }
        } catch (JMSException e) {
            e.printStackTrace();
            mdc.setRollbackOnly();
        } catch (Throwable te) {
            te.printStackTrace();
        }
        
    }
    
}
